var orderDetail = JSON.parse(sessionStorage.getItem("orderDetail"));
/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
var rejectSomeArray = {};//拒收列表对象
dataSource = [];
orderList(orderDetail)
function orderList(data) {
    dataSource.splice(0, dataSource.length);//清空数组
    $.each(data.listOrderItems, function (i, field) {
        var rowData = new Array();
        rowData.push(field.product_pic);
        rowData.push(field.warehouse_id);
        rowData.push(field.buyyer_count);
        rowData.push(field.delivery_count);
        rowData.push(field.can_returned_count);
        rowData.push(field.sku_code);
        rowData.push(field.product_name);
        rowData.push(field.sku_info);
        rowData.push(field.unit_name);
        rowData.push(field.sku_id);
        dataSource.push(rowData);
    });
    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": false,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                        var val = '<div onclick="goToGoodsDetail()" style="cursor:pointer;"><img style="float: left;" name="" src="http://www.xinfangsheng.com/resource/images/product/100/077360a1510620017665.jpg" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>'+ row[5] +'</div><div>'+ row[6] +'</div><div>'+ row[7] +'</div></div></div>';
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span>' + row[1] + '</span>';
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span>'+row[2]+ ''+row[8]+'</span>';
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span>'+row[1]+ ''+row[8]+'</span>';
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span>'+row[4]+ ''+row[8]+'</span>';
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span><input type="text" class="input-text mr-5" oninput="creatArray(this,\'some\')" value="" style="width: 100px" placeholder="" id="" name=""><span style="display: none">'+row[9]+'</span></span>'+ row[8];
                        return val;
                    },
                    "targets": 5
                }
            ]
        });
    };
}
function returnAll() {
    /*表格渲染*/
    var dataSource = new Array();
//table的数据源
    dataSource = [];
    orderList(orderDetail)
    function orderList(data) {
        dataSource.splice(0, dataSource.length);//清空数组
        $.each(data.listOrderItems, function (i, field) {
            var rowData = new Array();
            rowData.push(field.product_pic);
            rowData.push(field.warehouse_id);
            rowData.push(field.buyyer_count);
            rowData.push(field.delivery_count);
            rowData.push(field.can_returned_count);
            rowData.push(field.sku_code);
            rowData.push(field.product_name);
            rowData.push(field.sku_info);
            rowData.push(field.unit_name);
            rowData.push(field.sku_id);
            dataSource.push(rowData);
        });
        initDataTable(dataSource);

        function initDataTable(dataSource) {
            var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
            // dttable.fnClearTable(); //清空一下table
            dttable.fnDestroy(); //还原初始化了的datatable
            $("#datatable").DataTable({
                "autoWidth": false,
                "ordering": true,
                "searching": true,
                "paging": true,
                "retrieve": true,
                "destroy": true,
                "bAutoWidth": false,
                "bLengthChange": true,
                "aLengthMenu": [5, 10, 25, 50],
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    },
                    "sZeroRecords": "没有检索到数据",
                    "sProcessing": "<img src='./loading.gif' />"
                },
                data: dataSource,
                columnDefs: [
                    {
                        "visible": true,
                        "targets": 0
                    },
                    {
                        "render": function (data, type, row) {
                            var val = '<div onclick="goToGoodsDetail()" style="cursor:pointer;"><img style="float: left;" name="" src="http://www.xinfangsheng.com/resource/images/product/100/077360a1510620017665.jpg" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>'+ row[5] +'</div><div>'+ row[6] +'</div><div>'+ row[7] +'</div></div></div>';
                            return val;
                        },
                        "targets": 0
                    },
                    {
                        "render": function (data, type, row) {
                            var val = '<span>' + row[1] + '</span>';
                            return val;
                        },
                        "targets": 1
                    },
                    {
                        "render": function (data, type, row) {
                            var val = '<span>'+row[2]+ ''+row[8]+'</span>';
                            return val;
                        },
                        "targets": 2
                    },
                    {
                        "render": function (data, type, row) {
                            var val = '<span>'+row[3]+ ''+row[8]+'</span>';
                            return val;
                        },
                        "targets": 3
                    },
                    {
                        "render": function (data, type, row) {
                            var val = '<span>'+row[4]+ ''+row[8]+'</span>';
                            return val;
                        },
                        "targets": 4
                    },
                    {
                        "render": function (data, type, row) {
                            var val = '<span><input type="text" class="input-text mr-5" oninput="creatArray(this,\'all\')" value="'+row[4]+'" style="width: 100px" placeholder="" id="countInp" name=""><span style="display: none">'+row[9]+'</span></span>'+ row[8];
                            return val;
                        },
                        "targets": 5
                    }
                ]
            });
        };
    }
    for(var i=0;i<orderDetail.listOrderItems.length;i++){
        rejectSomeArray[orderDetail.listOrderItems[i].sku_id] = {
            sku_id: orderDetail.listOrderItems[i].sku_id,
            refund_count:orderDetail.listOrderItems[i].can_returned_count,
        }
    }
}

function creatArray(val,someOrAll) {
    if(val.value != "" || someOrAll == "some"){
        rejectSomeArray[val.nextElementSibling.innerHTML] = {
            sku_id: val.nextElementSibling.innerHTML,
            refund_count:val.value,
        }
    }else if(val.value != "" || someOrAll == "all"){
        rejectSomeArray[val.nextElementSibling.innerHTML].refund_count = val.value;
    }
}

function returnSome() {
    /*最终退款总金额*/
    var refund_amount_final = 0;
    var listOrderRefundItems = [];
    for(var key in rejectSomeArray){
        listOrderRefundItems.push(rejectSomeArray[key])
        refund_amount_final += Number(rejectSomeArray[key].subtotal)
    }
    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: root.orderInter.order_ordersuccess_refund,
        cache: false,  //禁用缓存
        dataType: "json",
        data: JSON.stringify({
            order_id:orderDetail.orderBase.order_id,
            refund_reason: $("#returnReason").val(),
            return_desc: $("#returnRemark").val(),
            take_goods_type: 10,
            detail_address:"河北省廊坊市标王制造有限公司",
            listOrderRefundItems: listOrderRefundItems
        }),
        success: function (result) {
            console.log(result);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}